package com.cloud.code.datetime;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjuster;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User: JIE
 * Date: 2021/4/26
 * Time: 21:43
 * Description: java8时间工具类练习
 */
@Data
@Slf4j
public class TimeDemo {


    @Test
    public void deme() {

        //获取今天的日期
        LocalDate localDate = LocalDate.now();
        log.info("今天的日期是：" + localDate);

        LocalDateTime localDateTime = LocalDateTime.now();
        log.info("今天的时间是：" + localDateTime);

        Date date = new Date();
        log.info(date.toString());

        //初始化一个日期.生日检查
        LocalDate birthday = LocalDate.of(1993, 11, 9);
        MonthDay birthdayMd = MonthDay.of(birthday.getMonth(), birthday.getDayOfMonth());
        YearMonth yearMonth = YearMonth.of(birthday.getYear(), birthday.getMonth());
        log.info(String.valueOf(birthday.getMonth()));
        log.info(String.valueOf(birthday.getDayOfMonth()));
        MonthDay today = MonthDay.from(LocalDate.of(2016, 10, 12));
        System.out.println(today.equals(birthdayMd));


        //日期主要是使用LocalTime，该类不包含日期，只有时间信息
        LocalTime localTime = LocalTime.now();
        log.info("现在的时间是：" + localTime);

        //如果不想显示毫秒
        LocalTime nowTime2 = LocalTime.now().withNano(0); //14:43:14
        log.info("现在的时间是：" + nowTime2);

        //指定时间
        LocalTime time = LocalTime.of(14, 10, 21); //14:10:21
        LocalTime time2 = LocalTime.parse("12:00:01"); // 12:00:01

        //当前时间增加2小时
        LocalTime nowTimePlus2Hour = time.plusHours(2); //16:47:23.144
        //或者
        LocalTime nowTimePlus2Hour2 = time.plus(2, ChronoUnit.HOURS);

        //日期前后比较
        LocalDate today1 = LocalDate.now();
        LocalDate specifyDate = LocalDate.of(2015, 10, 20);
        System.out.println(today1.isAfter(specifyDate)); //true

    }

    @Test
    public void zone() {

        //java8中，将日期，时间，时区都很好的进行了分离。
        //查看当前的时区
        ZoneId zoneId = ZoneId.systemDefault();
        System.out.println(zoneId);

        //查看美国纽约当前的时间
        ZoneId america = ZoneId.of("America/New_York");
        LocalDateTime shanghaiTime = LocalDateTime.now();
        LocalDateTime americaDateTime = LocalDateTime.now(america);
        System.out.println(shanghaiTime); //2016-11-06T15:20:27.996
        System.out.println(americaDateTime); //2016-11-06T02:20:27.996 ，可以看到美国与北京时间差了13小时

        //带有时区的时间
        ZonedDateTime americaZoneDateTime = ZonedDateTime.now(america);
        System.out.println(americaZoneDateTime); //2016-11-06T02:23:44.863-05:00[America/New_York]
    }

    /**
     * 时间差比较
     */
    @Test
    public void demo() {

        LocalDate today = LocalDate.now();
        LocalDate specifyDate = LocalDate.of(2015, 10, 2);
        Period period = Period.between(specifyDate, today);

        System.out.println(period.getDays());  //4
        System.out.println(period.getMonths()); //1
        System.out.println(specifyDate.until(today, ChronoUnit.DAYS)); //401


        String spDate = "20151011";
        DateTimeFormatter formatter = DateTimeFormatter.BASIC_ISO_DATE;
        LocalDate formatted = LocalDate.parse(spDate, formatter);
        System.out.println(formatted);

        //自定义格式
        DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("YYYY MM dd");
        System.out.println(formatter2.format(LocalDate.now()));

        //java8 时间类与Date类的相互转
        //Date与Instant的相互转化
        Instant instant = Instant.now();
        Date date = Date.from(instant);
        Instant instant2 = date.toInstant();

        //Date转为LocalDateTime
        Date date2 = new Date();
        LocalDateTime localDateTime2 = LocalDateTime.ofInstant(date2.toInstant(), ZoneId.systemDefault());

        //LocalDateTime转Date
        LocalDateTime localDateTime3 = LocalDateTime.now();
        Instant instant3 = localDateTime3.atZone(ZoneId.systemDefault()).toInstant();
        Date date3 = Date.from(instant);

        //LocalDate转Date
        //因为LocalDate不包含时间，所以转Date时，会默认转为当天的起始时间，00:00:00
        LocalDate localDate4 = LocalDate.now();
        Instant instant4 = localDate4.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant();
        Date date4 = Date.from(instant);
    }


}
